/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * VideoFrame.java
 *
 * Created on Apr 20, 2011, 12:51:12 AM
 */

package video.gui;

import audio.components.DecodeAndPlayAudio;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import video.components.DecodeAndPlayVideo;

/**
 *
 * @author Alex
 */
public class VideoFrame extends javax.swing.JFrame {

    private DecodeAndPlayVideo m_player = new DecodeAndPlayVideo();
    private DecodeAndPlayAudio m_audioPlayer = new DecodeAndPlayAudio();

    private final String FILENAME = "E:\\Torrent_Downloads\\Kill.the.Irishman.2011.DVDRip.AC3.XviD-CM8\\kti.xvid.avi";

    /** Creates new form VideoFrame */
    public VideoFrame() {
        initComponents();

        //m_image = java.awt.Toolkit.getDefaultToolkit().getImage("D:\\pix\\180004_177692212266911_100000784007548_338235_2289551_n.jpg");
/*
        try
        {
            MediaTracker mediaTr = new MediaTracker(this);
            mediaTr.addImage(m_image, 1);
            mediaTr.waitForID(1);

            int width = m_image.getWidth(null);
            int height = m_image.getHeight(null);

            setSize(width, height);
        }
        catch(InterruptedException ex)
        {
            ex.printStackTrace();
        }
*/

        this.createBufferStrategy(2);

        m_player.setVideoFrame(this);
        m_player.setFilename(FILENAME);
        m_audioPlayer.setFilename(FILENAME);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    public void setImage(BufferedImage bufImage) {
        
        setSize(bufImage.getWidth(), bufImage.getHeight());

        Graphics g = null;
        BufferStrategy bf = this.getBufferStrategy();

        try
        {
            g = bf.getDrawGraphics();
            g.drawImage(bufImage, 0, 0, null);
        }
        finally
        {
            g.dispose();
        }
        bf.show();
    }

    public void startVideo()
    {
        Thread videoTh = new Thread( new Runnable() {
            public void run() {

                m_player.decodeAndPlayVideo();

            }
        } );

        Thread audioTh = new Thread( new Runnable() {
            public void run() {

                m_audioPlayer.decodeAndPlayAudio();

            }
        } );

        videoTh.start();
        audioTh.start();
    }




    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables

}
